package com.czl.module_storehouse.mvp.model;

import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.base.data.net.RetrofitClient;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_storehouse.api.ApiService;
import com.czl.module_storehouse.bean.EditInventoryInfoBean;
import com.czl.module_storehouse.bean.InventoryBean;
import com.czl.module_storehouse.bean.InventoryInfoBean;
import com.czl.module_storehouse.bean.InventoryProductShiftBean;
import com.czl.module_storehouse.bean.InventoryProductStorageBean;
import com.czl.module_storehouse.bean.InventoryRoomBean;
import com.czl.module_storehouse.bean.RoomListBean;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author by xubin,
 * @date on 2022/2/10.
 * PS: Not easy to write code, please indicate.
 */
public class InventoryModel extends StorehouseLocationModel {

    public Observable<HttpResponse<InventoryInfoBean>> addInventoryInfo(InventoryInfoBean bean) {
        bean.setInventoryPersonId(getUserId());
        bean.setInventoryPersonName(getTName());
        bean.setStorehouseId(getStorehouseId());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).addInventoryInfo(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<Object>> productInventory(ProductBean bean) {
        bean.setStorehouseId(getStorehouseId());
        bean.setInventoryPersonId(getUserId());
        bean.setInventoryPersonName(getTName());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).productInventory(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<InventoryBean>> editInventoryInfo(EditInventoryInfoBean bean) {
        bean.setStorehouseId(getStorehouseId());
        bean.setInventoryPersonId(getUserId());
        bean.setInventoryPersonName(getTName());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).editInventoryInfo(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<Object>> inventoryProductShift(InventoryProductShiftBean bean) {
        bean.setStorehouseId(getStorehouseId());
        bean.setShiftPersonId(getUserId());
        bean.setShiftPersonName(getTName());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).inventoryProductShift(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<ProductBean>>> inventoryProductStorage(InventoryProductStorageBean bean) {
        bean.setStorehouseId(getStorehouseId());
        bean.setOperaterId(getUserId());
        bean.setOperaterName(getTName());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).inventoryProductStorage(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<LocatListBean>>> getNeedInventorySortInfo(int inventoryId, String roomId, String storehouseLocatId, EditInventoryInfoBean bean) {
        if (bean == null) {
            return getNeedInventorySortInfo2(inventoryId, roomId, storehouseLocatId);
        } else {
            return editInventoryInfo(bean).flatMap((Function<HttpResponse<InventoryBean>,
                    ObservableSource<HttpResponse<ListBean<LocatListBean>>>>) response -> {
                if (response.isSuccess()) {
                    return getNeedInventorySortInfo2(inventoryId, roomId, storehouseLocatId);
                }
                return Observable.error(new IOException(response.getMsg()));
            });
        }
    }

    public Observable<HttpResponse<ListBean<LocatListBean>>> getNeedInventorySortInfo2(int inventoryId, String roomId, String storehouseLocatId) {
        JSONObject object = new JSONObject();
        try {
            object.put("storehouseId", getStorehouseId());
            object.put("inventoryId", inventoryId);
            if (roomId != null && !roomId.isEmpty()) {
                object.put("roomId", roomId);
            }
            if (storehouseLocatId != null) {
                object.put("storehouseLocatId", storehouseLocatId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).getNeedInventorySortInfo(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<RoomListBean>>> getInventoryRoomInfo(int inventoryId) {
        JSONObject object = new JSONObject();
        try {
            object.put("inventoryId", inventoryId);
            object.put("storehouseId", getStorehouseId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).getInventoryRoomInfo(RequestBody.create(mediaType, json));
    }

    public Observable<InventoryRoomBean> getInventoryRoomList(int inventoryId) {
        return Observable.zip(getInventoryRoomInfo(inventoryId), getStorehouseLocatList(), (response, response2) -> {
            InventoryRoomBean bean = new InventoryRoomBean();
            bean.setRoomMsg(response.getErrorMsg());
            bean.setRoomListBeanListBean(response.getData());
            bean.setLocationMsg(response2.getErrorMsg());
            bean.setLocatListBeanListBean(response2.getData());
            return bean;
        });
    }

    public Observable<HttpResponse<ListBean<InventoryBean>>> getNeedDealtInvenList(int pageNo) {
        return RetrofitClient.getInstance().create(ApiService.class)
                .getNeedDealtInvenList(getRequestBodyByStorehouseIdPageNo(pageNo));
    }

    public Observable<HttpResponse<ListBean<InventoryBean>>> getDealtInvenList(int pageNo) {
        return RetrofitClient.getInstance().create(ApiService.class)
                .getDealtInvenList(getRequestBodyByStorehouseIdPageNo(pageNo));
    }
}
